<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="closeDialog"
    @open="opents"
  >
    <!-- 表单 -->
    <el-form
      ref="addForm"
      label-width="120px"
      :model="params"
      :rules="rules"
    >
      <el-form-item
        label="模板类型"
        prop="templateType"
      >
        <el-select
          v-model="params.templateType"
          style="width: 50%"
          placeholder="请选择运输类型"
        >
          <el-option
            label="同城寄"
            value="1"
          > </el-option>
          <el-option
            label="省内寄"
            value="2"
          > </el-option>
          <el-option
            label="经济区互寄"
            value="3"
          > </el-option>
          <el-option
            label="跨省寄"
            value="4"
          > </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="运送类型"
        prop="transportType"
      >
        <el-select
          v-model="params.transportType"
          style="width: 50%"
          placeholder="请选择运输类型"
        >
          <el-option
            label="普快"
            value="1"
          > </el-option>
          <el-option
            label="特快"
            value="2"
          > </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="关联城市"
        prop="associatedCityList"
        :rules="rules.associatedCityList"
      >
        <template v-if="params.templateType === '3'">
          <el-checkbox-group v-model="params.associatedCityList">
            <el-checkbox label="2">京津冀</el-checkbox>
            <el-checkbox label="3">江浙沪</el-checkbox>
            <el-checkbox label="4">川渝</el-checkbox>
            <el-checkbox label="5">黑吉辽</el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>
          <el-checkbox
            v-model="params.associatedCityList"
            label="1"
          >全国</el-checkbox>
        </template>
      </el-form-item>

      <el-form-item
        label="首重价格"
        prop="firstWeight"
      >
        <el-input
          v-model="params.firstWeight"
          style="width: 50%"
          placeholder="请选择首重价格"
        />
      </el-form-item>
      <el-form-item
        label="续重价格"
        prop="continuousWeight"
      >
        <el-input
          v-model="params.continuousWeight"
          style="width: 50%"
          placeholder="请选择续重价格"
        />
      </el-form-item>
      <el-form-item
        label="轻抛系数"
        prop="lightThrowingCoefficient"
      >
        <el-input
          v-model="params.lightThrowingCoefficient"
          style="width: 50%"
          placeholder="请选择轻抛系数"
        /> </el-form-item></el-form>
    <!-- footer插槽 -->
    <template
      v-slot:footer
    >
      <el-button
        type="warning"
        style="width: 100px;"
        @click="submitAdd"
      >确定</el-button>
      <el-button
        plain
        style="width: 100px;"
        @click="closeDialog"
      >取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { freightManageOperate, freightManageList } from '@/api/transit'
import carfre from '@/api/carfreight'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      visiable: false,
      params: {
        id: null, // 主键
        templateType: '', // 模板类型 模板类型：1-同城寄，2-省内寄，3-经济区互寄，4-跨省
        associatedCityList: [], // 关联城市：1-全国，2-京津冀，3-江浙沪，4-川渝，5-黑吉辽
        firstWeight: '', // 首重价格
        continuousWeight: '', // 续重价格
        lightThrowingCoefficient: '' // 轻抛系数
      },
      rules: {
        templateType: [
          { required: true, message: '模板类型不能为空', trigger: 'blur' }
        ],
        transportType: [
          { required: true, message: '运送类型不能为空', trigger: 'blur' }
        ],
        associatedCityList: [
          {
            required: true,
            type: 'array',
            min: 1,
            message: '请选择关联城市',
            trigger: ['blur', 'change']
          }
        ],
        firstWeight: [
          { required: true, message: '首重价格不能为空', trigger: 'blur' },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: '只能输入数字，最多保留两位小数',
            trigger: 'blur' }
        ],
        continuousWeight: [
          { required: true, message: '续重价格不能为空', trigger: 'blur' },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: '只能输入数字，最多保留两位小数',
            trigger: 'blur' }
        ],
        lightThrowingCoefficient: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value) {
                const reg = /^[1-9]+[0-9]*$/
                if (!reg.test(value)) {
                  callback(new Error('只能输入正整数，请重新输入'))
                } else {
                  callback()
                }
              } else {
                callback(new Error('轻抛系数不能为空'))
              }
            },
            trigger: 'blur'
          }
        ]
      },
      formatext: carfre.formatext,
      commatext: carfre.commatext,
      citylist: carfre.citylist
    }
  },

  computed: {
    showTitle() {
      return this.params && this.params.id ? '编辑模板' : '添加模板'
    },
    show() {
      return this.visiable
    }
  },
  _methods: {
    // 点击取消通知父组件
    closeDialog() {
      this.$refs.addForm.resetFields() // 重置校验结果

      // this.visiable = false
      this.params = {
        templateType: '',
        associatedCityList: [],
        firstWeight: '',
        continuousWeight: '',
        lightThrowingCoefficient: ''
      }
      const updatedShowDialog = false
      this.$emit('update:showDialog', updatedShowDialog)
      this.$emit('update:id', '')
    },
    updateSpan() {
      // 在这里可以根据需要更新数据
      this.spanValue = '新的值'
    },

    submitAdd() {
      this.$refs.addForm.validate(async (valid) => {
        if (
          this.params.templateType === 3 &&
      this.params.associatedCityList.length === 1 &&
      this.params.associatedCityList[0] === 1
        ) {
          return this.$message({
            message: '请选择关联城市',
            type: 'error'
          })
        }
        if (valid) {
          const data = {
            id: this.params.id,
            templateType: this.params.templateType,
            transportType: this.params.transportType,
            associatedCityList: this.params.associatedCityList,
            firstWeight: this.params.firstWeight,
            continuousWeight: this.params.continuousWeight,
            lightThrowingCoefficient: this.params.lightThrowingCoefficient
          }
          await freightManageOperate(data)
            .then((res) => {
              if (String(res.code) === '200') {
                this.$message({
                  message: '操作成功！',
                  type: 'success'
                })
                this.closeDialog()

                this.$emit('refresh') // 刷新列表
              } else {
                this.$message({
                  message: res.msg,
                  type: 'error'
                })
              }
            })
            .catch((error) => {
              console.error(error)
            })
        }
      })
    },
    // 打开表单
    async opents() {
      if (this.id) {
        const { data: res } = await freightManageList(this.id)
        console.log(res)
        const arr = res.filter((item) => {
          if (item.id === this.id) {
            return item
          }
        })
        this.params = arr[0]
        // console.log('this.params')
        // console.log(this.params)
        const TEMPLATETYPE = {
          1: '同城寄',
          2: '省内寄',
          3: '经济区互寄',
          4: '跨省寄'
        }
        this.params.templateType = TEMPLATETYPE[this.params.templateType]
        const TRANSPORTTYPE = {
          1: '普快',
          2: '特快'
        }
        this.params.transportType = TRANSPORTTYPE[this.params.transportType]
      }
    }
  },
  get methods() {
    return this._methods
  },
  set methods(value) {
    this._methods = value
  }
}
</script>

<style>
</style>
